package com.aletto.general_store.ui.adapters;

import android.app.Activity;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;

import com.aletto.general_store.R;
import com.aletto.general_store.data.DataRepository;
import com.aletto.general_store.data.model.Address;
import com.aletto.general_store.presenters.core.SimpleTemplate;
import com.aletto.general_store.utils.event.UseAddressEvent;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.sisyphus.spinach.core.INetTask;
import com.sisyphus.spinach.network.presenters.NetSingleTask;
import com.sisyphus.spinach.network.views.Debounce;
import com.sisyphus.spinach.network.views.ToastView;

import androidx.annotation.NonNull;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

public class AddressListAdapter extends BaseQuickAdapter<Address, BaseViewHolder> {

    private final int type;

    public AddressListAdapter(int type) {
        super(R.layout.item_address);
        this.type = type;

        addChildClickViewIds(R.id.tv_delete, R.id.tv_use_address, R.id.radio_default_settings);

        setOnItemChildClickListener((a, view, position) -> {
            AddressListAdapter adapter = (AddressListAdapter) a;

            if (view.getId() == R.id.tv_delete) { // 删除
                delete(view, adapter, position);

            } else if (view.getId() == R.id.radio_default_settings) {
                default_settings(view, adapter, position);

            } else if (view.getId() == R.id.tv_use_address) {
                EventBus.getDefault().post(new UseAddressEvent(adapter.getItem(position)));
                ((Activity) view.getContext()).finish();
            }
        });
    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, Address address) {
        holder.setText(R.id.tv_address, address.getAddress())
                .setVisible(R.id.tv_use_address, type != 2)
                .setText(R.id.tv_user_info, address.getName() + "\t\t\t" + address.getPhone());


        TextView radio_default_settings = holder.getView(R.id.radio_default_settings);
        radio_default_settings.setSelected(address.getIs_default() == Address.DEFAULT);
        if (address.getIs_default() == Address.DEFAULT) {
            radio_default_settings.setText(R.string.address_default);

        } else {
            radio_default_settings.setText(R.string.set_address_normal);
        }
    }

    private void default_settings(View view, AddressListAdapter adapter, int position) {
        Address item = adapter.getItem(position);
        new SimpleTemplate<>() {

            @Override
            public INetTask.View<Object> view() {
                return new Debounce<>(view, new ToastView<>(view.getContext()) {
                    @Override
                    public void complete() {
                        super.complete();

                        int index = getIndex();

                        if (index != -1) {
                            adapter.notifyItemChanged(index);
                        }

                        item.setIs_default(Address.DEFAULT);

                        adapter.notifyItemChanged(position);
                    }

                    private int getIndex() {
                        int index = -1;

                        List<Address> data = adapter.getData();
                        for (int i = 0; i < data.size(); i++) {
                            Address address = data.get(i);

                            if (address.getIs_default() == Address.DEFAULT) {
                                index = i;
                            }

                            address.setIs_default(Address.NOT_DEFAULT);
                        }

                        return index;
                    }
                });
            }

            @Override
            public INetTask.Presenter<Object, INetTask.View<Object>> presenter() {
                return new NetSingleTask<>(DataRepository.getInstance().defaultAddress(item.getId()));
            }

        }.execute();
    }

    private void delete(View view, AddressListAdapter adapter, int position) {
        Address item = adapter.getItem(position);
        new SimpleTemplate<>() {

            @Override
            public INetTask.View<Object> view() {
                return new Debounce<>(view, new ToastView<>(view.getContext()) {
                    @Override
                    public void complete() {
                        super.complete();
                        adapter.remove(item);
                    }
                });
            }

            @Override
            public INetTask.Presenter<Object, INetTask.View<Object>> presenter() {
                return new NetSingleTask<>(DataRepository.getInstance().deleteAddress(item.getId()));
            }

        }.execute();
    }

}
